package com.ipan.weixin.msg.general;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 视频消息
 * 
 * @author iPan
 * @version 2013-12-23
 */
public class VideoMsg extends PlainMsg {

	/** serialVersionUID */
	private static final long serialVersionUID = 4895769203155889487L;
	
	/** 视频消息媒体id（必须） */
	protected String mediaId;
	
	/** 视频消息缩略图的媒体id */
	protected String thumbMediaId;
	
	/** 视频消息的标题 */
	protected String title;
	
	/** 视频消息的描述 */
	protected String description;
	
	public VideoMsg() {
		super(TransportType.RECEIVE_AND_REPLY);
	}
	
	public VideoMsg(Document doc) {
		this();
		fromXml(doc);
	}

	public String getMediaId() {
		return mediaId;
	}

	public void setMediaId(String mediaId) {
		this.mediaId = mediaId;
	}

	public String getThumbMediaId() {
		return thumbMediaId;
	}

	public void setThumbMediaId(String thumbMediaId) {
		this.thumbMediaId = thumbMediaId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public void fromXml(Document doc) {
		if (doc == null) {
			return ;
		}
		
		super.fromXml(doc);
		Element root = doc.getRootElement();
		this.mediaId = root.elementTextTrim(EL_MEDIA_ID);
		this.thumbMediaId = root.elementTextTrim(EL_THUMB_MEDIA_ID);
		this.title = root.elementTextTrim(EL_TITLE);
		this.description = root.elementTextTrim(EL_DESCRIPTION);
	}
	
	@Override
	public String toXml(boolean nonEmpty) {
		StringBuilder buf = new StringBuilder("<xml>");
		buf.append(super.toXml(nonEmpty));
		buf.append(wrapElementHead(EL_MSG_TYPE)).append(TYPE_VIDEO).append(wrapElementEnd(EL_MSG_TYPE));
		if (nonEmpty) {
			buf.append(wrapElementHead(EL_VIDEO));
			if (StringUtils.isNotEmpty(this.mediaId)) {	
				buf.append(wrapElementHead(EL_MEDIA_ID)).append(this.mediaId).append(wrapElementEnd(EL_MEDIA_ID));
			}
			if (StringUtils.isNotEmpty(this.title)) {
				buf.append(wrapElementHead(EL_TITLE)).append(this.title).append(wrapElementEnd(EL_TITLE));
			}
			if (StringUtils.isNotEmpty(this.description)) {
				buf.append(wrapElementHead(EL_DESCRIPTION)).append(this.description).append(wrapElementEnd(EL_DESCRIPTION));
			}
			buf.append(wrapElementEnd(EL_VIDEO));
		} else {
			buf.append(wrapElementHead(EL_VIDEO));
			buf.append(wrapElementHead(EL_MEDIA_ID)).append(getStringValue(this.mediaId)).append(wrapElementEnd(EL_MEDIA_ID));
			buf.append(wrapElementHead(EL_TITLE)).append(getStringValue(this.title)).append(wrapElementEnd(EL_TITLE));
			buf.append(wrapElementHead(EL_DESCRIPTION)).append(getStringValue(this.description)).append(wrapElementEnd(EL_DESCRIPTION));
			buf.append(wrapElementEnd(EL_VIDEO));
		}
		return buf.append("</xml>").toString();
	}

}

/*---------------------------------------------
接收：
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1357290913</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<MediaId><![CDATA[media_id]]></MediaId>
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
<MsgId>1234567890123456</MsgId>
</xml>

回复：
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<Video>
<MediaId><![CDATA[media_id]]></MediaId>
<Title><![CDATA[title]]></Title>
<Description><![CDATA[description]]></Description>
</Video> 
</xml>
--------------------------------------------*/